package com.lance.answer.util.image_valid;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import sun.misc.BASE64Encoder;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


@Slf4j
public class VerifyCodeUtils {

    // cxLjljbAX3L97bsTWMlBjxlRIkafrzAi
    private static String API = "https://codevirify.market.alicloudapi.com/icredit_ai_image/verify_code/v1";
  //  private static String APP_CODE = "714bd2277d3444af858ded6813cdabf5";
    private static String APP_CODE = "714bd2277d3444af858ded6813cdabf5";

    public static void main(String[] args) throws Exception{
        String src = "";
        System.err.println(read(src));
    }

    public static String GetImageStr(String imgFile)
    {//将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        InputStream in = null;
        byte[] data = null;
        //读取图片字节数组
        try
        {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        //对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);//返回Base64编码过的字节数组字符串
    }


    public static String read(String image) throws IOException {
        CloseableHttpClient httpClient = null;
        String verifyCode = null;
        try{
            httpClient = HttpClients.createDefault();
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("IMAGE", image));
            params.add(new BasicNameValuePair("IMAGE_TYPE", "0"));
            HttpPost httpPost = new HttpPost(API);
            httpPost.addHeader("Authorization","APPCODE " + APP_CODE);
            httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

            // 设置请求参数
            httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
            // 发送GET请求
            HttpResponse execute = httpClient.execute(httpPost);
            // 获取状态码
            int statusCode = execute.getStatusLine().getStatusCode();
            System.out.println(statusCode);
            // 获取结果
            HttpEntity entity = execute.getEntity();
            String result = EntityUtils.toString(entity, "utf-8");
            JSONObject object = JSON.parseObject(result);
            verifyCode = object.getJSONObject("VERIFY_CODE_ENTITY").getString("VERIFY_CODE");
            log.info("识别结果如下：");
            log.info(verifyCode);
        }catch (Exception e) {
           log.error("识别验证码出错，图片编码：{}",image,e);
        }finally{
            if (httpClient != null) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return verifyCode;
    }

}
